package org.example.physics_engine.geometry;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

//间隔，是一个一维数值，实际上就是一个范围，有两个值决定
@Data
@Getter
@Setter
@AllArgsConstructor
public class Interval {

    //左边界坐标
    protected double min;
    //有边界坐标
    protected double max;

    //判断两个Interval是否重叠
    public boolean overlap(Interval interval) {
        return !(this.min > interval.max || interval.min > this.max);
    }

    //返回两个Interval的重叠长度
    public double getOverlap(Interval interval) {
        if (this.overlap(interval)) {
            return Math.min(this.max, interval.max) - Math.max(this.min, interval.min);
        }
        return 0;
    }

    //判断参数Interval是否包含于本Interval
    public boolean containsExclusive(Interval interval) {
        return interval.min > this.min && interval.max < this.max;
    }

    //返回夹在其他两个数字之间的数字
    //夹逼定理？
    /**
     * Returns a number clamped between two other numbers.
     * <p>
     * This method assumes that min &le; max.
     * @param value the value to clamp
     * @param left the min value
     * @param right the max value
     * @return double
     */
    public static double clamp(double value, double left, double right) {
//        if (value <= max && value >= min) {
//            return value;
//        } else if (value < min) {
//            return min;
//        } else {
//            return max;
//        }
        return (value <= right && value >= left) ? value : (value < left ? left : right);
    }

}
